package com.tmall.qatest.converter;

import com.taobao.hsf.notify.client.message.BytesMessage;
import com.taobao.hsf.notify.client.message.Message;
import com.taobao.hsf.notify.extend.TypedMessageConverter;
import com.tmall.qatest.info.DpcMessageInfo;
import com.tmall.qatest.util.MessageUtil;

/**
 * Created with IntelliJ IDEA.
 * User: Shuffle
 * Date: 13-2-5
 * Time: 上午10:31
 * To change this template use File | Settings | File Templates.
 */
public class DpcMessageConverter extends TypedMessageConverter<DpcMessageInfo> {
    private String messageTopic;
    private String messageType;



    public DpcMessageConverter() {
        super();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public String getMessageTopic() {
        return this.messageTopic;
    }

    @Override
    public String getMessageType() {
        return this.messageType;
    }

    @Override
    protected Message toTypedMessage(DpcMessageInfo dpcMessageInfo) {
        if(null == dpcMessageInfo){
            throw new NullPointerException("purchaseOrder cannot be null!");
        }
        BytesMessage messages = new BytesMessage();
        messages.setTopic(getMessageTopic());
        messages.setMessageType(getMessageType());
        messages.setStringProperty("customHead","customValue");
        messages.setPostTimeout(30000);

        messages.setBody(MessageUtil.object2ByteArray(dpcMessageInfo));

        return messages;
    }

    @Override
    public DpcMessageInfo fromMessage(Message message) {
        DpcMessageInfo dpcMessageInfo = null;
        if(message instanceof BytesMessage){
            dpcMessageInfo = (DpcMessageInfo)MessageUtil.byteArray2Object(((BytesMessage) message).getBody());
            dpcMessageInfo.setMessage(message);

        }
        return dpcMessageInfo;
    }
}
